package com.huadao.pda.common.adapter;

import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.TextView;

public class CommonViewHolder extends RecyclerView.ViewHolder implements View.OnLongClickListener, View.OnClickListener {

    private SparseArray<View> viewSparseArray;
    private onItemCommonClickListener commonClickListener;

    public CommonViewHolder(View itemView) {
        super(itemView);
        itemView.setOnClickListener(this);
        itemView.setOnLongClickListener(this);
        viewSparseArray = new SparseArray<>();
    }

    public <T extends View> T getView(int viewId){
        View view=viewSparseArray.get(viewId);
        if(view==null){
            view=itemView.findViewById(viewId);
            viewSparseArray.put(viewId,view);
        }
        return (T) view;
    }

    public CommonViewHolder setText(int viewId,CharSequence text){
        TextView tv=getView(viewId);
        tv.setText(text);
        return this;
    }

    public CommonViewHolder setTextColor(int viewId,int color){
        TextView tv=getView(viewId);
        tv.setTextColor(color);
        return this;
    }

    public CommonViewHolder setBackgroung(int viewId,int resId){
        View view=getView(viewId);
        view.setBackgroundResource(resId);
        return this;
    }

    public CommonViewHolder setChecked(int viewId,boolean checked){
        CompoundButton compoundButton=getView(viewId);
        compoundButton.setChecked(checked);
        return this;
    }

    public CommonViewHolder setCheckedChangeListener(int viewId,CompoundButton.OnCheckedChangeListener onCheckedChangeListener){
        CompoundButton compoundButton=getView(viewId);
        compoundButton.setOnCheckedChangeListener(onCheckedChangeListener);
        return this;
    }

    public CommonViewHolder setEnable(int viewId,boolean enable){
        View v=getView(viewId);
        v.setEnabled(enable);
        return this;
    }

    public CommonViewHolder setOnClickListener(int viewId, View.OnClickListener onClickListener){
        View v=getView(viewId);
        v.setOnClickListener(onClickListener);
        return this;
    }

//    protected interface onItemCommonClickListener{
//        void onItemClickListener(int position);
//        void onItemLongClickListener(int position);
//    }

    public void setCommonClickListener(onItemCommonClickListener commonClickListener) {
        this.commonClickListener = commonClickListener;
    }

    public CommonViewHolder onClickListener(int viewId, View.OnClickListener onClickListener){
        View view=getView(viewId);
        view.setOnClickListener(onClickListener);
        return this;
    }

    public CommonViewHolder setVisibility(int viewId, int visibility){
        View view=getView(viewId);
        view.setVisibility(visibility);
        return this;
    }

    @Override
    public void onClick(View v) {
        if(commonClickListener!=null){
            commonClickListener.onItemClickListener(getAdapterPosition());
        }
    }

    @Override
    public boolean onLongClick(View v) {
        if(commonClickListener!=null){
            commonClickListener.onItemLongClickListener(getAdapterPosition());
            return true;
        }
        return false;
    }

}
